<template>
  <div v-loading="isLoading">
    <div class="weather_container">
      <div class="BAC-title" style="height: 68px;font-size: 24px;">水上桥区管控专题</div>
      <div class="content-container">
        <div class="content">
          <div class="title">偏航统计</div>
          <div id="lineChart1" class="echarts"></div>
        </div>
        <div class="content">
          <div class="title">超高统计</div>
          <div id="lineChart2" class="echarts"></div>
        </div>
        <div class="content">
          <div class="title">超速统计</div>
          <div id="lineChart3" class="echarts"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from "vue";

import * as echarts from "echarts/core";
import {
  TooltipComponent,
  GridComponent,
  LegendComponent,
  DataZoomComponent,
} from "echarts/components";
import { LineChart } from "echarts/charts";
import { UniversalTransition } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";
echarts.use([
  TooltipComponent,
  GridComponent,
  LegendComponent,
  DataZoomComponent,
  LineChart,
  CanvasRenderer,
  UniversalTransition,
]);

// 引入api
import { yawStatistics } from "../../../utils/bridgeAreaControlApi";
import { useRouter } from "vue-router";
const router = useRouter();
const isLoading = ref(false);
const xAxisData1 = ref([]);
const xAxisData2 = ref([]);
const xAxisData3 = ref([]);
const seriesData1 = ref([]);
const seriesData2 = ref([]);
const seriesData3 = ref([]);

const getyawStatisticsFn = (data) => {
  isLoading.value = true;
  yawStatistics(data)
    .then((res) => {
      if (res.code === 200) {
        res.data.pianhang.forEach((item1) => {
          xAxisData1.value.push(item1.month);
          seriesData1.value.push(item1.times);
        });
        res.data.chaogao.forEach((item2) => {
          xAxisData2.value.push(item2.month);
          seriesData2.value.push(item2.times);
        });
        res.data.chaosu.forEach((item3) => {
          xAxisData3.value.push(item3.month);
          seriesData3.value.push(item3.times);
        });
      }
      nextTick(() => {
        initLineChart1();
        initLineChart2();
        initLineChart3();
      });
    })
    .finally(() => {
      isLoading.value = false;
    });
};
const initLineChart1 = () => {
  let lineChart = echarts.getInstanceByDom(
    document.getElementById("lineChart1")
  );
  if (lineChart != null && lineChart != "" && lineChart != undefined) {
    lineChart.dispose();
  }
  lineChart = echarts.init(document.getElementById("lineChart1"));

  let option;
  if(xAxisData1.value.length === 0){
    option = {
      title: {
        text: "暂无数据",
        x: "center",
        y: "center",
        textStyle: {
          color: "#8c8c8c",
          fontSize: 12,
          fontWeight: 500
        }
      }
    };
  }else{
    option = {
      tooltip: {
        trigger: "axis",
        confine: true, //限制在图表区域内
      },
      grid: {
        top: "40px",
        left: "45px",
        right: "30px",
        bottom: "45px",
      },
      dataZoom: [
        {
          show: false,
          type: "inside",
          startValue: 0,
          endValue: 7,
          zoomLock: false,
        },
      ],
      xAxis: {
        type: "category",
        // data: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
        data: xAxisData1.value,
        // name: "月份",
        nameTextStyle: {
          fontWeight: "500",
          fontSize: "10px",
          color: "rgba(0,0,0,0.45)",
        },
        axisLabel: {
          interval: 0,
          rotate: -30, //倾斜的程度
        },
      },
      yAxis: {
        type: "value",
        name: "船舶数量(艘)",
      },
      series: [
        {
          name: "船舶数量(艘)",
          data: seriesData1.value,
          type: "line",
          color: "#6B9BFD",
        },
      ],
    };
  }
  option && lineChart.setOption(option);
};
const initLineChart2 = () => {
  let lineChart = echarts.getInstanceByDom(
    document.getElementById("lineChart2")
  );
  if (lineChart != null && lineChart != "" && lineChart != undefined) {
    lineChart.dispose();
  }
  lineChart = echarts.init(document.getElementById("lineChart2"));

  let option;
  if(xAxisData2.value.length === 0){
    option = {
      title: {
        text: "暂无数据",
        x: "center",
        y: "center",
        textStyle: {
          color: "#8c8c8c",
          fontSize: 12,
          fontWeight: 500
        }
      }
    };
  }else{
    option = {
      tooltip: {
        trigger: "axis",
        confine: true, //限制在图表区域内
      },
      grid: {
        top: "40px",
        left: "45px",
        right: "30px",
        bottom: "45px",
      },
      dataZoom: [
        {
          show: false,
          type: "inside",
          startValue: 0,
          endValue: 7,
          zoomLock: false,
        },
      ],
      xAxis: {
        type: "category",
        // data: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
        data: xAxisData2.value,
        // name: "月份",
        nameTextStyle: {
          fontWeight: "500",
          fontSize: "10px",
          color: "rgba(0,0,0,0.45)",
        },
        axisLabel: {
          interval: 0,
          rotate: -30, //倾斜的程度
        },
      },
      yAxis: {
        type: "value",
        name: "船舶数量(艘)",
      },
      series: [
        {
          name: "船舶数量(艘)",
          data: seriesData2.value,
          type: "line",
          color: "#4ED9A0",
        },
      ],
    };
  }
  option && lineChart.setOption(option);
};
const initLineChart3 = () => {
  let lineChart = echarts.getInstanceByDom(
    document.getElementById("lineChart3")
  );
  if (lineChart != null && lineChart != "" && lineChart != undefined) {
    lineChart.dispose();
  }
  lineChart = echarts.init(document.getElementById("lineChart3"));

  let option;
  if(xAxisData3.value.length === 0){
    option = {
      title: {
        text: "暂无数据",
        x: "center",
        y: "center",
        textStyle: {
          color: "#8c8c8c",
          fontSize: 12,
          fontWeight: 500
        }
      }
    };
  }else{
    option = {
      tooltip: {
        trigger: "axis",
        confine: true, //限制在图表区域内
      },
      grid: {
        top: "40px",
        left: "45px",
        right: "30px",
        bottom: "45px",
      },
      dataZoom: [
        {
          show: false,
          type: "inside",
          startValue: 0,
          endValue: 7,
          zoomLock: false,
        },
      ],
      xAxis: {
        type: "category",
        // data: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
        data: xAxisData3.value,
        // name: "月份",
        nameTextStyle: {
          fontWeight: "500",
          fontSize: "10px",
          color: "rgba(0,0,0,0.45)",
        },
        axisLabel: {
          interval: 0,
          rotate: -30, //倾斜的程度
        },
      },
      yAxis: {
        name: "船舶数量(艘)",
        type: "value",
      },
      series: [
        {
          name: "船舶数量(艘)",
          data: seriesData3.value,
          type: "line",
          color: "#F17665 ",
        },
      ],
    };
  }
  option && lineChart.setOption(option);
};

onMounted(() => {
  let bridge = router?.currentRoute?.value?.query?.bridge;
  getyawStatisticsFn({ bridge });
});
</script>

<style lang="scss" scoped>
::-webkit-scrollbar {
  display: none;
}
.weather_container {
  height: 100vh;
  overflow: hidden;
  .filter {
    display: flex;
    margin: 0px 0px 16px 0px;
    background-color: #fff;
    width: 100%;
    position: absolute;
    top: 68px;
    left: 0;
    z-index: 10;
    padding: 5px 20px;
    box-sizing: border-box;
    .selectClass {
      width: calc(50% - 5px);
    }
    .selectClass:nth-child(1) {
      margin-right: 10px;
    }
  }
  margin: 0 16px;
  .content-container {
    margin-top: 78px;
    overflow-y: scroll;
    height: calc(100vh - 68px);
  }
  .content {
    background: #ffffff;
    box-shadow: 0px 4px 8px 2px rgba(0, 0, 0, 0.08);
    border-radius: 8px;
    margin-bottom: 16px;
    overflow: hidden;
    .title {
      display: flex;
      background: rgba(22, 93, 255, 0.05);
      height: 36px;
      color: #262626;
      padding: 0 8px;
      align-items: center;
      border-bottom: 1px solid #e6e6e6;
      font-size: 14px;
    }
    .echarts {
      height: 316px;
    }
  }
}
.BAC-title {
  width: 100%;
  font-size: 24px;
  line-height: 68px;
  background: rgba(49, 118, 233, 0.6);
  color: #fff;
  text-align: center;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 10;
  background: url("../../../static/images/bridgeAreaControl/bg_zhuanti.png")
    no-repeat;
  background-size: cover;
}
::v-deep.van-field {
  background-color: #f8f9fb;
  border-radius: 8px;
}
</style>
